package com.kawa.kemanage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kawa.kemanage.entity.dto.RegisterDTO;
import com.kawa.kemanage.entity.User;
import com.kawa.kemanage.mapper.UserMapper;
import com.kawa.kemanage.service.UserService;
import com.kawa.kemanage.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Map<String, Object> login(String username, String password) {
        // 2. 查询用户
        User user = (User) userMapper.findByUserName(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        // 3. 验证密码
        if (!user.getPassword().equals(password)) {
            throw new RuntimeException("密码错误");
        }

        // 4. 生成token
        String token = JwtUtil.generateToken(user.getId(), user.getUserName());

        // 5. 返回登录信息
        Map<String, Object> loginInfo = new HashMap<>();
        loginInfo.put("id",user.getId());
        loginInfo.put("token", token);
        loginInfo.put("userName", user.getUserName());
        loginInfo.put("userType", user.getUserType());
        loginInfo.put("id",user.getId());

        return loginInfo;
    }

    /**
     * 查询用户列表
     * @param pageNo
     * @param pageSize
     * @param excludeUserType
     * @param filterType
     * @param userName
     * @return
     */
    @Override
    public PageInfo<User> UserList(Integer pageNo, Integer pageSize, Integer excludeUserType, Integer filterType, String userName) {
        // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        // 查询数据
        List<User> users = userMapper.queryUserList(excludeUserType, filterType, userName);
        // 返回分页信息
        return new PageInfo<>(users);
    }

    /**
     *添加用户
     * @param user
     * @return
     */
    @Override
    public boolean addUser(User user) {
        // 设置默认用户类型为普通用户
        if (user.getUserType() == null) {
            user.setUserType(1);
        }
        return (userMapper.addUser(user) > 0);
    }

    /**
     * 删除用户
     * @param id 用户ID
     * @param currentUserType 当前操作用户的类型
     * @return
     */
    @Override
    public int deleteUser(Integer id, Integer currentUserType) {
        // 如果是人事部用户，检查要删除的用户是否为管理员或超级管理员
        if (currentUserType == 5) {
            User user = userMapper.findById(id);
            if (user == null || user.getUserType() == 2 || user.getUserType() == 3) {
                throw new RuntimeException("人事部不能删除管理员或超级管理员");
            }
        }

        int resultNum = 0;
        try {
            resultNum = userMapper.deleteUser(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultNum;
    }

    /**
     * 修改用户
     * @param user 用户信息
     * @param currentUserType 当前操作用户的类型
     * @return
     */
    @Override
    public boolean updateUser(User user, Integer currentUserType) {
        // 如果是人事部用户，检查要修改的用户是否为管理员或超级管理员
        if (currentUserType == 5) {
            User existingUser = userMapper.findById(user.getId().intValue());
            if (existingUser == null || existingUser.getUserType() == 2 || existingUser.getUserType() == 3) {
                throw new RuntimeException("人事部不能修改管理员或超级管理员");
            }
            // 检查新设置的用户类型是否合法
            if (user.getUserType() == 2 || user.getUserType() == 3) {
                throw new RuntimeException("人事部不能将用户设置为管理员或超级管理员");
            }
        }
        return userMapper.updateUser(user) > 0;
    }

    /**
     * 获取用户类型列表
     * @return 用户类型列表
     */
    @Override
    public List<Map<String, Object>> queryUserTypeList() {
        return userMapper.queryUserTypeList();
    }

    /**
     * 获取个人中心的信息
     * @param id
     * @return
     */
    @Override
    public User personalCenter(Integer id) {
        return userMapper.personalCenter(id);
    }

    /**
     * 修改个人信息
     * @param user
     * @return
     */
    @Override
    public boolean updatePersonal(User user) {
        return  userMapper.updateUserPerson(user)>0;
    }

    @Override
    public boolean udatePassword(User user) {
        return userMapper.udatePassword(user)>0;
    }

    @Override
    public boolean udatePhone(User user) {
        return userMapper.udatePhone(user)>0;
    }

    @Override
    public boolean register(RegisterDTO registerDTO) {
        if (userMapper.findByUserName(registerDTO.getUsername())==null){
            User user = new User();
            user.setUserName(registerDTO.getUsername());
            user.setPassword(registerDTO.getPassword());
            user.setEmail(registerDTO.getEmail());
            user.setCreateDate(new Date());
            user.setUserType(1);
            return userMapper.addUser(user)>0;
        }
        return false;
    }


    /**
     * 获取用户关注的人数据
     * @param follower_id
     * @return
     */
    @Override
    public List<User> getFollowees(Integer follower_id) {
        return userMapper.getFollowees(follower_id);
    }

    //获取粉丝数据
    @Override
    public List<User> getFollower(Integer followee_id) {
        return userMapper.getFollower(followee_id);
    }



    //取消关注
    @Override
    public boolean delFollowee(Integer follow_id) {
        return userMapper.delFollee(follow_id)>0;
    }

    @Override
    public boolean addFollowee(User user) {
        return userMapper.addFollee(user)>0;
    }
}
